import { defineStore } from "pinia"
import { ref } from "vue"

import useProductStore from "./product"

export default defineStore('cart', () => {

    let productStore = useProductStore()


    let carts = ref([])

    let addToCart = item => {
  

    
        let productTarget = productStore.products.find(productItem => productItem.id === item.id)

     

        productTarget && productTarget.inventory--

   

   

        let target = carts.value.find(v => v.id === item.id)

       
        if (target) {
            target.count++
        } else {
           
            let cartItem = {
                id: item.id,
                title: item.title,
                price: item.price,
              
                count: 1
            }
          
            carts.value.push(cartItem)
        }
    }


    return { carts, addToCart }
})